package amartin47;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class LyricsClient extends Thread {

	public boolean stopClient = false;
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    Socket socket = null;
    String lyrics = "";
    LyricsWindow owner;
    
    public LyricsClient(LyricsWindow owner){
   		 //dateServer = new ServerSocket(3000);
    	this.owner = owner;
        this.start();
    }
    
    
	public void run()
	{
		try
		{
			while(!stopClient) {
		         
	 	        // open I/O streams for objects
				socket = new Socket("localhost", 3000);
	 	        oos = new ObjectOutputStream(socket.getOutputStream());
	 	        ois = new ObjectInputStream(socket.getInputStream());
	 	        // read an object from the server
	 	        String newLyrics = (String) ois.readObject();
	 	        oos.close();
	 	        ois.close();
	 	        
	 	        if(!newLyrics.equals(lyrics))
	 	        {
	 	        	this.lyrics = newLyrics;
	 	        	this.owner.updateSlide(this.lyrics);
	 	        }
	 	        Thread.sleep(150);
	         } 
	     }
        catch(Exception e) 
        {
       	 	e.printStackTrace();
        }
		finally
		{
		    System.out.println("Stop Client");
 	        try {
				socket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
